﻿using ET.DBCommon.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET.DBCommon.DAL
{
    public class PaperTestQuestionTaskDAL
    {
        public static List<PaperTestQuestionTaskModel> SelectTestQuestionTaskList()
        {
            string sql = "select * from testQuestionTask order by `Order`";
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<PaperTestQuestionTaskModel> lst = new List<PaperTestQuestionTaskModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    PaperTestQuestionTaskModel info = GetTestQuestionTaskModel(dt.Rows[i]);
                    lst.Add(info);
                }
            }
            return lst;
        }

        public static bool InsertTestQuestionTask(out int newId, PaperTestQuestionTaskModel testQuestionTask)
        {
            string sql = "INSERT INTO `paper_testquestion_task` (`TestId`,`Describes`,`MutexTask`,`AfterTask`,`AfterInterval`,`Order`,UIOrder,TaskType,IsMain)" +
                "values (@TestId,@Describes,@MutexTask,@AfterTask,@AfterInterval,@Order,@UIOrder,@TaskType,@IsMain)";
            MySqlParameter[] parms = {
                new MySqlParameter("@TestId", testQuestionTask.TestId),
                new MySqlParameter("@Describes", testQuestionTask.Describes),
                new MySqlParameter("@MutexTask", testQuestionTask.MutexTask),
                new MySqlParameter("@AfterTask", testQuestionTask.AfterTask),
                new MySqlParameter("@AfterInterval", testQuestionTask.AfterInterval),
                new MySqlParameter("@Order", testQuestionTask.Order),
                new MySqlParameter("@UIOrder", testQuestionTask.UIOrder),
                new MySqlParameter("@TaskType", testQuestionTask.TaskType),
                 new MySqlParameter("@IsMain", testQuestionTask.IsMain)//Add by lukun 主线分支
            };

            return DbHelper.ExecuteNonQuery(out newId, sql, parms) > 0;
        }

        public static bool UpdateTestQuestionTask(PaperTestQuestionTaskModel testQuestionTask, bool unChangeTaskDic = true)
        {
            string sql = "update paper_testquestion_task set Describes=@Describes,MutexTask=@MutexTask,`AfterTask`=@AfterTask,AfterInterval=@AfterInterval,`Order`=@Order," +
                "UIOrder=@UIOrder,`TaskType`=@TaskType,`IsMain`=@IsMain WHERE Id = @Id";
            MySqlParameter[] parms = {
                new MySqlParameter("@Describes", testQuestionTask.Describes),
                new MySqlParameter("@MutexTask", testQuestionTask.MutexTask),
                new MySqlParameter("@AfterTask", testQuestionTask.AfterTask),
                new MySqlParameter("@AfterInterval", testQuestionTask.AfterInterval),
                new MySqlParameter("@Order", testQuestionTask.Order),
                new MySqlParameter("@UIOrder", testQuestionTask.UIOrder),
                new MySqlParameter("@TaskType", testQuestionTask.TaskType),
                 new MySqlParameter("@IsMain", testQuestionTask.IsMain),
                new MySqlParameter("@Id", testQuestionTask.Id)
            };
            return DbHelper.ExecuteNonQuery(sql, parms) > 0;
        }

        public static bool DeleteTestQuestionTask(int Id)
        {
            string sql = "delete from paper_testquestion_task WHERE Id = @Id;delete from paper_testquestion_task_step where TaskId=@Id";
            MySqlParameter[] parms = {
                new MySqlParameter("@Id", Id)
            };
            return DbHelper.ExecuteNonQuery(sql, parms) > 0;
        }

        public static List<PaperTestQuestionTaskModel> GetTasksByTestQuestionID(int TaskId)
        {
            string sql = "select * from paper_testquestion_task where TaskId = " + TaskId + "  order by `Order`";
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<PaperTestQuestionTaskModel> lst = new List<PaperTestQuestionTaskModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    PaperTestQuestionTaskModel info = GetTestQuestionTaskModel(dt.Rows[i]);

                    lst.Add(info);
                }
            }
            return lst;
        }
        public static List<PaperTestQuestionTaskModel> GetNoIsOtherTasksByTestQuestionID(int TaskId)
        {
            string sql = "select * from paper_testquestion_task where TaskId = " + TaskId +  " order by `Order`";
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<PaperTestQuestionTaskModel> lst = new List<PaperTestQuestionTaskModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    PaperTestQuestionTaskModel info = GetTestQuestionTaskModel(dt.Rows[i]);

                    lst.Add(info);
                }
            }
            return lst;
        }
        public static List<PaperTestQuestionTaskModel> GetTasksByQuestionID(int TestId)
        {
            string sql = "select * from paper_testquestion_task where TestId = " + TestId + "  order by `Order`";
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<PaperTestQuestionTaskModel> lst = new List<PaperTestQuestionTaskModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    PaperTestQuestionTaskModel info = GetTestQuestionTaskModel(dt.Rows[i]);
                    lst.Add(info);
                }
            }
            return lst;
        }

        public static PaperTestQuestionTaskModel GetTaskByID(int Id)
        {
            string sql = "select * from paper_testquestion_task where Id = " + Id;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            PaperTestQuestionTaskModel info = null;
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    info = GetTestQuestionTaskModel(dt.Rows[i]);
                }
            }
            return info;
        }


        private static PaperTestQuestionTaskModel GetTestQuestionTaskModel(DataRow dr)
        {
            PaperTestQuestionTaskModel info = new PaperTestQuestionTaskModel();
            info.Id = Convert.ToInt32(dr["Id"]);
            info.TestId = Convert.ToInt32(dr["TestId"]);
            info.Describes = dr["Describes"].ToString();
            if (dr["MutexTask"] != DBNull.Value)
                info.MutexTask = int.Parse(dr["MutexTask"].ToString());
            if (dr["AfterTask"] != DBNull.Value)
                info.AfterTask = int.Parse(dr["AfterTask"].ToString());
            if (dr["AfterInterval"] != DBNull.Value)
                info.AfterInterval = int.Parse(dr["AfterInterval"].ToString());
            info.Order = Convert.ToInt32(dr["Order"]);
            if (dr["UIOrder"] != DBNull.Value)
                info.UIOrder = int.Parse(dr["UIOrder"].ToString());
            if (dr["TaskType"] != DBNull.Value)
                info.TaskType = dr["TaskType"].ToString();
            if (dr["IsMain"] != DBNull.Value)
                info.IsMain = Convert.ToBoolean(dr["IsMain"]) ;//Add by lukun\]
            return info;
        }
    }
}
